{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1f2d7701",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    "> 1. 创建FaceSet\n",
    "> 2. 查询FaceSet\n",
    "> 3. 存入face_token数据\n",
    "> 4. 获取FaceSet信息数据\n",
    "> 5. 人脸搜索\n",
    "> 6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18349beb",
   "metadata": {},
   "source": [
    "# 1. 创建人脸集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7650b7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b615ca7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d2cac422",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    \n",
    "}\n",
    "r = requests.post( url=url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cf2215ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "39f2d1ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'a8c850bbaef527a32a7a9064cbd4836b',\n",
       " 'time_used': 181,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648993857,84b49579-6e55-4594-a158-6f9edc705482',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae61957b",
   "metadata": {},
   "source": [
    "# 2. 查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dfd86553",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "48f8f248",
   "metadata": {},
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    \n",
    "}\n",
    "r = requests.post( url=getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "89d099e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 84,\n",
       " 'facesets': [{'faceset_token': 'fceee5436e64c826dee8484d1f4f33cd',\n",
       "   'outer_id': 'API0102',\n",
       "   'display_name': '新闻网新党支部 64人 48女',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'a8c850bbaef527a32a7a9064cbd4836b',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648993994,f5f564dd-cea1-48d4-b289-2749ff74ebc2'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "49ee516e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fceee5436e64c826dee8484d1f4f33cd'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token=r.json()['facesets'][0]['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c961285e",
   "metadata": {},
   "source": [
    "# 3. 更新"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c820fe0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id':\"API0102\",\n",
    "    'display_name':\"新闻网新党支部 64人 48女\",\n",
    "}\n",
    "r = requests.post( url=update_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bcc566c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'fceee5436e64c826dee8484d1f4f33cd',\n",
       " 'request_id': '1648993986,560a2a39-829f-43d9-8be0-3964f7452d72',\n",
       " 'time_used': 72,\n",
       " 'outer_id': 'API0102'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0032e48d",
   "metadata": {},
   "source": [
    "# 4. 获取所有的FaceSet集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9aa028ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 291,\n",
       " 'facesets': [{'faceset_token': 'fceee5436e64c826dee8484d1f4f33cd',\n",
       "   'outer_id': 'API0102',\n",
       "   'display_name': '新闻网新党支部 64人 48女',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'a8c850bbaef527a32a7a9064cbd4836b',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648994471,7b6f7862-daad-43c8-9414-1459d3ba60f7'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url, params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7473af68",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
